﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Gfw.BLL.Basic.User;
using Gfw.Web.Models.User;
using Gfw.Web.Models.Users;

namespace Gfw.Web.Controllers
{
    public class UserController : Controller
    {
        #region 用户登录
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            //  UserGenealogyLogic.GetFamily("sfsdf");
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }


        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(UserLoginModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Session["ValidateCode"].ToString() != model.VerifyCode)
                        throw new Exception("验证码不正确。");
                    if (L.Manage.Authentication.Login(model.UserName, EncryptAlgorithmHelper.Encrypt(model.Password, model.UserName)))
                    {
                        // FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Default");
                        }
                    }

                    throw new Exception("用户名或者密码错误或者无权限");

                }
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        #endregion
    }
}
